//
//  pico_nixie
//
//  Created by Pineapple, 2021/02/24.
//  Copyright @ 2021 Pineapple. All rights reserved.
//

#include "hv5523.h"
#include "mpu6050.h"
#include "apds_9960.h"
#include "max668.h"
#include "input.h"
#include "rtc.h"
#include "wifi.h"
#include "bt.h"
#include "oled.h"
#include <soc/soc.h>
#include <soc/rtc_cntl_reg.h>

typedef struct{
  int sender;
  char *msg;
}Data;//队列数据结构体

xQueueHandle xqueue0;  //创建的测试队列句柄，我们定义数据为int型
xQueueHandle xqueue1;  //创建的测试队列句柄，我们定义数据为字符串型
xQueueHandle xqueue2;  //创建的测试队列句柄，我们定义数据为结构体型，结构体里面包括一个int型以及一个字符串型
TaskHandle_t xTask0;   //任务0的句柄
TaskHandle_t xTask1;   //任务1的句柄

void setup()
{
    WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);//关闭低电压检测，避免不断重启
    Serial.begin(115200);

    Serial.print("Setup Priority： ");
    Serial.println(uxTaskPriorityGet(NULL));

    xqueue0 = xQueueCreate( 10, sizeof( int ) );
    xqueue1 = xQueueCreate( 10, sizeof( char[10] ) );
    xqueue2 = xQueueCreate(10, sizeof(Data));
  
    xTaskCreatePinnedToCore(Task1, "Task1", 10000, NULL, 11, &xTask0, 0);  //最后一个参数至关重要，决定这个任务创建在哪个核上.PRO_CPU 为 0, APP_CPU 为 1,或者 tskNO_AFFINITY 允许任务在两者上运行.
    xTaskCreatePinnedToCore(Task2, "Task2", 10000, NULL, 12, &xTask1, 1);  //xTaskGetAffinity(xTask1)  查询任务被固定到哪里

    Serial.print("xTask0 Priority: ");
    Serial.println(uxTaskPriorityGet(xTask0));  //优先级获取函数
    Serial.print("xTask1 Priority: ");
    Serial.println(uxTaskPriorityGet(xTask1));  //优先级获取函数
    if(xqueue0 != NULL && xqueue1 != NULL && xqueue2 != NULL)
        Serial.println("Start Queue Test!!!");
    Serial.println("******************************************************************");

    Serial.print("PineWatch Start.\r\n");
    Serial.print("PineWatch Running.\r\n");
    
    MAX668_Init();
    HV5523_Init();
    MPU6050_Init();
    APDS_9960_Init();
    Input_Init();

    rtc.setTime(0,30,12,1,4,2021);//Sec min hour date month year
    
    HV_EN;delay(70);
    Nixie_Fresh();
    delay(70);
    Display_Time();
    Display_Voltage();
    HV_SD;
}

void Task1(void *pvParameters)
{   
    while(1)
    {
        HV_EN;
        Nixie_Fresh();
        HV_SD;
        Serial.print("Nixie Fresh function running on core: ");
        Serial.println(xPortGetCoreID());
        vTaskDelay(3000);
    }
}//end of Core0

void Task2(void *pvParameters)
{
    
    while(1)
    {
        MPU6050_Print();
        Serial.print("MPU6050 Print function running on core: ");
        Serial.println(xPortGetCoreID());
        vTaskDelay(1000);
    }
}//end of Core1

void loop()
{   
}//end of loop
